<?php
class Model_Membre extends Model_WotHub {
    
    public static $_baseUri = 'http://api.worldoftanks.eu/wot/account/';
    
    public static function getMembreInfo($account_id){
        $url = self::$_baseUri.'info/?application_id='.$_SESSION['appId'].'&account_id='.$account_id;
        $data_joueur = self::api_get_content($url, 'accountinfo.json');
        $data = json_decode($data_joueur, true);
        return $data['data'][$account_id];
    }
    
    public static function getListeTank($account_id) {
        $url = 'http://api.worldoftanks.eu/wot/tanks/stats/?application_id='.$_SESSION['appId'].'&fields=tank_id,all,in_garage,mark_of_mastery&account_id='.$account_id;
    	$listeTank = self::api_get_content($url, 'statsperso.json');
    	$tanks = json_decode($listeTank, true);
    	return $tanks['data'][$account_id];
    }
    
    public static function getMembreId() {
        return $_COOKIE['account_id'];
    }
    
    public static function getClanId($account_id) {
        if (isset($_SESSION['accountInfo']['clanId']) === true) {
            return $_SESSION['accountInfo']['clanId'];
        }
        $membre = self::getMembreInfo($account_id);
        return $membre['clanId'];
    }
    
    public static function getNickName($pseudo) {
        $url = self::$_baseUri.'info/?application_id='.$_SESSION['appId'].'&account_id='.$pseudo.'&fields=nickname';
        $listePseudo = self::api_get_content($url, 'pseudo.json');
        $pseudos =  json_decode($listePseudo, true);
    	return $pseudos['data'];
    }
    
    public function isStrateur($clanId, $membreId) {
        $sql = "select clan_id from strateurs where clan_id = :clanid and account_id = :membreid";
        $test = $this->executerSql($sql, array(':clanid' => $clanId, ':membreid' => $membreId));
        if (count($test) === 0) {
            return 'ko';
        }
        return true;
    }
    
    public function isOfficier($role) {
        if (in_array($role, array('recruit','private')) === true) {
            return 'ko';
        }
        return true;
    }
    
    public static function getPersonnalRating($accountId) {
       $stats = self::getMembreInfo($accountId);
       $personnalStats = $stats['statistics']['all'];
       $personnalStats['winRate'] = round(($personnalStats['wins']/$personnalStats['battles'])*100,2);
       $personnalStats['tiers'] = Ratings::getAvgTier($accountId)[$accountId];
       $personnalStats = Ratings::addRatios($personnalStats, $accountId);
       return $personnalStats;
    }
    
    private function _deletePersonalStat($accountId) {
        $this->executerSql('DELETE FROM tankstats WHERE account_id = :accountid', array(':accountid' => $accountId));        
    }
    
    public function insertPersonalStats($accountId, $data) {
        $this->_deletePersonalStat($accountId);
        foreach ($data as $tank) {
            $champs = implode(',', array_keys($tank['all']));
            $values = implode(',', array_values($tank['all']));
            $champs.= ',mark_of_mastery, tank_id, account_id';
            $values.= ','.$tank['mark_of_mastery'].','.$tank['tank_id'].','.$accountId;
            $this->executerSql("INSERT INTO tankstats ($champs) VALUES ($values)");
        }
    }
}